<?php
/*
 * 分页类
 * @author andery <andery@foxmail.com>
 * @link http://www.Nimo.com/
 * @version $Id: CPage.lib.php 2010-07-17 10:37:10Z andery $
 */
class CPage
{
    // 数据总数
    private $total_nums;
    // 每页显示的数据数
    private $list_nums;
    // 总页面数
    private $total_page;
    // URL中表示页面的参数名
    private $param_name;
    // 当前页
    private $page_alt;
    // 分页栏显示数
    private $page_show_nums;
    // 分页栏栏数
    private $page_bar_nums;
    // 分页组件保存数组
    private $com_arr = array();
    
    public function __construct($total_nums, $list_nums='10', $param_name='p', $page_show_nums='5')
    {
        CLang::load('lib.Page');
        $this->total_nums = intval($total_nums);
        $this->list_nums = intval($list_nums);
        $this->page_show_nums = intval($page_show_nums);
        $this->param_name = $param_name;
        $this->page_alt = !empty($_GET[$param_name]) ? intval($_GET[$param_name]) : 1;
        $this->total_page = ceil($this->total_nums/$this->list_nums);
        $this->page_bar_nums  = ceil($this->total_page/$this->page_show_nums);
        $this->page_bar_alt = ceil($this->page_alt/$this->page_show_nums);// 当前分页栏的栏数
    }
    
    /**
     * 获得查语句中的limit信息
     */
    public function limit()
    {
        $start = ($this->page_alt-1)*$this->list_nums;
        $len = ($this->total_nums < ($start+$this->list_nums)) ? $this->total_nums-$start : $this->list_nums;
        return "{$start},{$len}";
    }
    
    public function show($theme = '1')
    {
        $params = Nimo::app()->get_UrlParams();
        $m = Nimo::app()->getController();
        $a = Nimo::app()->getAction();
        unset($params['m']);
        unset($params['a']);
        unset($params[$this->param_name]);
        if (isset($_GET['noum']) && $_GET['noum'] == '1') {
            $params['noum'] = '1';
        }
        // 构建分页显示组件
        $this->com_arr = array();
        // 获取本样式语言项目
        $lang = CLang::get('pagebar_'.$theme);
        if (!is_array($lang)) {
            $lang = CLang::get('pagebar_1');
        }
        // 数据总数
        $this->com_arr['com_total']  = $lang['page_total_before'];
        $this->com_arr['com_total'] .= "<span>".$this->total_nums."</span>";
        $this->com_arr['com_total'] .= $lang['page_total_after'];
        // 上一页
        if ($this->page_alt-1 > 0) {
            $params[$this->param_name] = $this->page_alt-1;
            if ($params[$this->param_name] == 1) {
                unset($params[$this->param_name]);
            }
            $prev_url = Nimo::app()->create_url("{$m}/{$a}", $params);
            $this->com_arr['com_prev'] = "<a href='".$prev_url."' class='page_prev'>".$lang['page_prev']."</a>";
        } else {
            $this->com_arr['com_prev'] = '';
        }
        // 下一页
        if ($this->page_alt+1 <= $this->total_page){
            $params[$this->param_name] = $this->page_alt+1;
            $next_url = Nimo::app()->create_url("{$m}/{$a}", $params);
            $this->com_arr['com_next'] = "<a href='".$next_url."' class='page_next'>".$lang['page_next']."</a>";
        }else{
            $this->com_arr['com_next'] = '';
        }
        // 首页
        if($this->page_bar_alt > 1){
            //$params[$this->param_name] = '1';
            unset($params[$this->param_name]);
            $first_url = Nimo::app()->create_url("{$m}/{$a}", $params);
            $this->com_arr['com_first'] = "<a href='".$first_url."' class='page_first'>".$lang['page_first']."</a>";
        }else{
            $this->com_arr['com_first'] = "";
        }
        // 尾页
        if($this->page_bar_nums && $this->page_bar_alt != $this->page_bar_nums){
            $params[$this->param_name] = $this->total_page;
            $last_url = Nimo::app()->create_url("{$m}/{$a}", $params);
            $this->com_arr['com_last'] = "<a href='".$last_url."'  class='page_last'>".$lang['page_last']."</a>";
        }else{
            $this->com_arr['com_last'] = "";
        }
        // 分页栏
        $this->com_arr['com_list'] = '';
        if ($this->total_page>1) {
            $offset = ceil($this->page_show_nums/2-1);
            if ($this->page_show_nums > $this->total_page) {
			    $from = 1;
			    $to = $this->total_page;
		    } else {
			    $from = $this->page_alt - $offset;
			    $to = $from + $this->page_show_nums - 1;
			    if($from < 1) {
				    $to = $this->page_alt + 1 - $from;
				    $from = 1;
				    if($to - $from < $this->page_show_nums) {
					    $to = $this->page_show_nums;
				    }
			    } elseif($to > $this->total_page) {
				    $from = $this->total_page - $this->page_show_nums + 1;
				    $to = $this->total_page;
			    }
		    }
            for($i = $from; $i <= $to; $i++) {
			    $class = ($i==$this->page_alt) ? 'page_list current' : 'page_list';
			    $params[$this->param_name] = $i;
			    if ($i == 1) {
			        unset($params[$this->param_name]);
			    }
                $list_url = Nimo::app()->create_url("{$m}/{$a}", $params);
                $this->com_arr['com_list'] .= "<a href='".$list_url."' class='".$class."'>".$i."</a>";
		    }
        }
        $page_bar_str = '';
        eval('$page_bar_str = $this->page_theme_'.$theme.'();');
        return $page_bar_str;
    }
    
    public function show_ajax($fun, $theme = '1')
    {
        $params = Nimo::app()->get_UrlParams();
        $m = Nimo::app()->getController();
        $a = Nimo::app()->getAction();
        unset($params['m']);
        unset($params['a']);
        unset($params[$this->param_name]);
        if (isset($_GET['noum']) && $_GET['noum'] == '1') {
            $params['noum'] = '1';
        }
        // 构建分页显示组件
        $this->com_arr = array();
        // 获取本样式语言项目
        $lang = CLang::get('pagebar_'.$theme);
        if (!is_array($lang)) {
            $lang = CLang::get('pagebar_1');
        }
        // 数据总数
        $this->com_arr['com_total']  = $lang['page_total_before'];
        $this->com_arr['com_total'] .= "<span>".$this->total_nums."</span>";
        $this->com_arr['com_total'] .= $lang['page_total_after'];
        // 上一页
        if ($this->page_alt-1 > 0) {
            $p = $this->page_alt-1;
            $this->com_arr['com_prev'] = "<a href=\"javascript:{$fun}('$p');\" class=\"page_prev\">".$lang['page_prev']."</a>";
        } else {
            $this->com_arr['com_prev'] = '';
        }
        // 下一页
        if ($this->page_alt+1 <= $this->total_page){
            $p = $this->page_alt+1;
            $this->com_arr['com_next'] = "<a href=\"javascript:{$fun}('$p');\" class=\"page_next\">".$lang['page_next']."</a>";
        }else{
            $this->com_arr['com_next'] = '';
        }
        // 首页
        if($this->page_bar_alt > 1){
            $this->com_arr['com_first'] = "<a href=\"javascript:{$fun}('1');\" class=\"page_first\">".$lang['page_first']."</a>";
        }else{
            $this->com_arr['com_first'] = "";
        }
        // 尾页
        if($this->page_bar_nums && $this->page_bar_alt != $this->page_bar_nums){
            $p = $this->total_page;
            $this->com_arr['com_last'] = "<a href=\"javascript:{$fun}('$p');\"  class=\"page_last\">".$lang['page_last']."</a>";
        }else{
            $this->com_arr['com_last'] = "";
        }
        // 分页栏
        $this->com_arr['com_list'] = '';
        if ($this->total_page>1) {
            $offset = ceil($this->page_show_nums/2-1);
            if ($this->page_show_nums > $this->total_page) {
			    $from = 1;
			    $to = $this->total_page;
		    } else {
			    $from = $this->page_alt - $offset;
			    $to = $from + $this->page_show_nums - 1;
			    if($from < 1) {
				    $to = $this->page_alt + 1 - $from;
				    $from = 1;
				    if($to - $from < $this->page_show_nums) {
					    $to = $this->page_show_nums;
				    }
			    } elseif($to > $this->total_page) {
				    $from = $this->total_page - $this->page_show_nums + 1;
				    $to = $this->total_page;
			    }
		    }
            for($i = $from; $i <= $to; $i++) {
			    $class = ($i==$this->page_alt) ? 'page_list current' : 'page_list';
			    $p = $i;
                $this->com_arr['com_list'] .= "<a href=\"javascript:{$fun}('$p');\" class=\"".$class."\">".$i."</a>";
		    }
        }
        $page_bar_str = '';
        eval('$page_bar_str = $this->page_theme_'.$theme.'();');
        return $page_bar_str;
    }
    
    private function page_theme_1()
    {
        return $this->com_arr['com_first']
        .$this->com_arr['com_prev']
        .$this->com_arr['com_list']
        .$this->com_arr['com_next']
        .$this->com_arr['com_last'];
    }
    
    
    private function page_theme_2()
    {
        return $this->com_arr['com_prev'].'<span>'.$this->page_alt.'/'.$this->total_page.'</span>'.$this->com_arr['com_next'];
    }
    
    private function page_theme_3()
    {
    }
}